package com.yzx.lifeassistants.model.analysis;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.XML;

import com.yzx.lifeassistants.bean.Lecture;
import com.yzx.lifeassistants.utils.CalendarUtil;
import com.yzx.lifeassistants.utils.LogcatUtils;

/**
 * @Description: 解析查询讲座返回的结果
 * @author: yzx
 * @time: 2016-3-8 上午10:26:44
 */
public class QueryLectureAnalysis {

	/**
	 * 
	 * @Description: 结果是否为空
	 */
	public static Boolean isNull(String response) {
		Boolean isNull = true;
		try {
			JSONObject jsonObject = XML.toJSONObject(response);
			JSONObject listObject = jsonObject.getJSONObject("list");
			if (listObject.isNull("act")) {
				isNull = true;
			} else {
				isNull = false;
			}
		} catch (JSONException e) {
			if (null != e.getMessage()) {
				LogcatUtils.e("解析查询讲座返回的结果异常", e.getMessage());
			}
		}
		return isNull;
	}

	/**
	 * 
	 * @Description: 解析返回的讲座列表
	 */
	public static List<Lecture> getData(String response) {
		List<Lecture> lectures = new ArrayList<Lecture>();
		try {
			JSONObject jsonObject = XML.toJSONObject(response);
			JSONObject listObject = jsonObject.getJSONObject("list");
			JSONArray array = listObject.getJSONArray("act");
			for (int i = 0; i < array.length(); i++) {
				JSONObject object = array.getJSONObject(i);
				Lecture lecture = new Lecture();
				lecture.setId(object.getInt("id"));
				lecture.setTimestamp(CalendarUtil.longToString(
						"yyyy-MM-dd HH:mm:ss", object.getLong("timestamp")));
				lecture.setBeginTime(CalendarUtil.longToString(
						"yyyy-MM-dd HH:mm:ss", object.getLong("beginTime")));
				lecture.setActivityPlace(object.getString("activityPlace"));
				lecture.setSpeaker(object.getString("speaker"));
				lecture.setSponsor(object.getString("sponsor"));
				lecture.setContent(object.getString("content"));
				lectures.add(lecture);
			}
		} catch (JSONException e) {
			if (null != e.getMessage()) {
				LogcatUtils.e("解析查询讲座返回的结果异常", e.getMessage());
			}
		}
		return lectures;

	}
}
